﻿#include "TBUTask.h"
#include "services/tlv/Com/TlvThreadCom.h"

#include <QDebug>


#define BLOCK_SIZE  128
#define APP_SIZE    0xA800
#define TOTAL_SIZE  0x15000


TBUTask::TBUTask(QObject *parent)
    : ITask{parent}
{
    setTaskName(tr("TestBoxUpdate"));
}

void TBUTask::setMemData(const QByteArray &block)
{
    blockApp.clear();
    blockBoot.clear();
    if (block.length() < TOTAL_SIZE) {
        emit errorOccurred(tr("Hex file size is too small (%1Bytes at least)!").arg(TOTAL_SIZE));
        stop();
        return;
    }

    // 获取APP数据
    uint8_t data[128] = {
        0x00, 0xa9, 0xff, 0xff, 0xa9, 0xa9, 0xc4, 0xa9,  0xdf, 0xa9, 0xfa, 0xa9, 0x15, 0xaa, 0x30, 0xaa,
        0x4b, 0xaa, 0x66, 0xaa, 0x81, 0xaa, 0x9c, 0xaa,  0xb7, 0xaa, 0xd2, 0xaa, 0xed, 0xaa, 0x08, 0xab,
        0x23, 0xab, 0x3e, 0xab, 0x59, 0xab, 0x74, 0xab,  0x8f, 0xab, 0xaa, 0xab, 0xc5, 0xab, 0xe0, 0xab,
        0xfb, 0xab, 0x16, 0xac, 0x31, 0xac, 0x4c, 0xac,  0x67, 0xac, 0x82, 0xac, 0x9d, 0xac, 0xb8, 0xac,
        0xd3, 0xac, 0xee, 0xac, 0x09, 0xad, 0x24, 0xad,  0x3f, 0xad, 0x5a, 0xad, 0x75, 0xad, 0x90, 0xad,
        0xab, 0xad, 0xc6, 0xad, 0xe1, 0xad, 0xfc, 0xad,  0x17, 0xae, 0x32, 0xae, 0x4d, 0xae, 0x68, 0xae,
        0x83, 0xae, 0x9e, 0xae, 0xb9, 0xae, 0xd4, 0xae,  0xef, 0xae, 0x0a, 0xaf, 0x25, 0xaf, 0x40, 0xaf,
        0x5b, 0xaf, 0x76, 0xaf, 0x91, 0xaf, 0xac, 0xaf,  0xc7, 0xaf, 0xe2, 0xaf, 0xfd, 0xaf, 0x18, 0xb0,
    };
    MemBlock tmp = { 0x00, QByteArray(reinterpret_cast<char*>(data), 128) };
    blockApp.push_back(tmp);
    tmp = {0x80, block.mid(0x80, BLOCK_SIZE)};
    blockApp.push_back(tmp);
    for (uint32_t addr = 0x0400; 1;addr += BLOCK_SIZE) {
        if (addr >= APP_SIZE) break;
        tmp = {addr, block.mid(addr, BLOCK_SIZE)};
        blockApp.push_back(tmp);
    }
    tmp = {0x00, block.mid(0, BLOCK_SIZE)};
    blockApp.push_back(tmp);
    tmp = {0x80, block.mid(0x80, BLOCK_SIZE)};
    blockApp.push_back(tmp);


    // 获取BOOT数据
    for (uint32_t addr = APP_SIZE; 1;addr += BLOCK_SIZE) {
        if (addr >= TOTAL_SIZE) break;
        tmp = {addr, block.mid(addr, BLOCK_SIZE)};
        blockBoot.push_back(tmp);
    }
}

void TBUTask::mcuReset()
{
    TlvThreadCom* com = TlvThreadCom::instance();
    Tlv in(TlvTag::Tlv_MCU_RESET);
    Tlv out;
    if (!com->syncWrite(in, out)) {
        stop();
    }
}

void TBUTask::enterBoot()
{
    TlvThreadCom* com = TlvThreadCom::instance();
    Tlv in(TlvTag::Tlv_MCU_Enter_BOOT);
    Tlv out;
    if (!com->syncWrite(in, out)) {
        stop();
    }
}

bool TBUTask::isInMode(uint8_t mode)
{
    TlvThreadCom* com = TlvThreadCom::instance();
    Tlv in(TlvTag::Tlv_MCU_BOOT_APP_REQ);
    Tlv out;
    if (!com->syncWrite(in, out)) {
        stop();
        return false;
    }
    if (out.value().isEmpty()) {
        stop();
        return false;
    }
    return out.value().at(0) == mode;
}


bool TBUTask::flash(MemBlock &block)
{
    TlvThreadCom* com = TlvThreadCom::instance();
    QByteArray data = ui32ToBytesBE(block.addr);
    data += block.data;
    Tlv in(TlvTag::Tlv_MCU_Program_REQ, data);
    Tlv out;
    if (!com->syncWrite(in, out)) {
        emit errorOccurred(tr("Flash failed!"));
        return false;
    }
    return true;
}

void TBUTask::runTask()
{
    emit hasDone(0);

    uint32_t total = blockApp.length() + blockBoot.length();
    uint32_t finish = 0;

    if (total == 0) return;


    mcuReset();
    QThread::msleep(500);
    if (!isInMode(AppMode)) {
        stop();
        return;
    }

    // 处于APP,开始刷写App
    for (auto& b : blockApp) {
        if (flash(b)) {
            finish++;
            emit hasDone((int)(100*finish / total));
        } else {
            return;
        }
    }

    mcuReset();
    QThread::msleep(500);
    enterBoot();
    if (!isInMode(BootMode)) {
        stop();
        return;
    }

    // 处于Boot,开始刷写Boot
    for (auto& b : blockBoot) {
        if (flash(b)) {
            finish++;
            emit hasDone((int)(100*finish / total));
        } else {
            return;
        }
    }

    mcuReset();
    QThread::msleep(500);
}
